//******************************************************************************
//this code is protected by the GNU affero GPLv3
//author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
//                        <digital.ragnarok AT gmail dot com>
//******************************************************************************
#include "globals.h"

static s8 add(struct client *c,s32 region_slot,struct msg *m)
{
  s8 r;
  if(m->req_dws<6){
    PERR("client(%d):fatal:region::add missing data\n",c->so);
    r=LWL_ERR;goto exit;
  }

  LOG_WIRE("client(%d):region::add x=%d y=%d w=%d h=%d\n",c->so,(s32)m->req[2],
                                  (s32)m->req[3],(s32)m->req[4],(s32)m->req[5]);

  r=region_add(c,region_slot,(s32)m->req[2],(s32)m->req[3],(s32)m->req[4],
                                                                (s32)m->req[5]);

exit:
  return r;
}

static s8 sub(struct client *c,s32 region_slot,struct msg *m)
{
  s8 r;
  if(m->req_dws<6){
    PERR("client(%d):fatal:region::sub missing data)\n",c->so);
    r=LWL_ERR;goto exit;
  }

  LOG_WIRE("client(%d):region::sub x=%d y=%d w=%d h=%d\n",c->so,(s32)m->req[2],
                                  (s32)m->req[3],(s32)m->req[4],(s32)m->req[5]);

  r=region_sub(c,region_slot,(s32)m->req[2],(s32)m->req[3],(s32)m->req[4],
                                                                (s32)m->req[5]);
exit:
  return r;
}

s8 wl_region_req(struct client *c,s32 region_slot,struct msg *m)
{
  s8 r;
  switch(m->req_op){
  case WL_REGION_DESTROY:
    LOG_WIRE("client(%d):region::destroy region_id=%u\n",c->so,m->req[0]);
    r=region_destroy(c,region_slot);
    break;
  case WL_REGION_ADD:
    r=add(c,region_slot,m);
    break;
  case WL_REGION_SUBTRACT:
    r=sub(c,region_slot,m);
    break;
  default:
    PERR("client(%d):fatal:region unknown opcode %u\n",c->so,m->req_op);
    r=LWL_ERR;
  };
  return r;
}
